package com.luge.model.download;

import java.util.HashMap;

import com.luge.model.download.DownLoad.DownLoadCallBack;
import com.luge.utils.FileManager;

public class DownLoadManager {
	public static DownLoadManager downLoadManager = null;
	private static HashMap<String, DownLoad> downs;

	public static DownLoadManager getInstance() {
		if (downLoadManager == null) {
			downLoadManager = new DownLoadManager();
			downs = new HashMap<String, DownLoad>();
		}
		return downLoadManager;
	}

	public void add(String urlPath, String filePath,
			DownLoadCallBack downLoadCallBack) {
		if (downs.containsKey(urlPath)) {
			downs.get(urlPath).add(downLoadCallBack);
		} else if (FileManager.getInstance().isFileExist(filePath)) {
			downLoadCallBack.success(urlPath);
		} else {
			DownLoad downLoad = new DownLoad(urlPath, filePath, true);
			downLoad.add(downLoadCallBack);
			downs.put(urlPath, downLoad);
			downLoad.start();
		}
	}

	public boolean isExsit(String urlPath) {
		return downs.containsKey(urlPath);
	}

	public void remove(String urlPath) {
		downs.remove(urlPath);
	}

	public DownLoad getDown(String urlPath) {
		if (urlPath != null && downs.containsKey(urlPath)) {
			return downs.get(urlPath);
		} else {
			return null;
		}
	}
}
